//
//  ElectricWaveView.m
//  safeLady
//
//  Created by xmy on 2017/1/11.
//  Copyright © 2017年 xmy. All rights reserved.
//

#import "ElectricWaveView.h"
@interface ElectricWaveView ()
@property (weak, nonatomic) IBOutlet UIImageView *electricCable_img;
@property (weak, nonatomic) IBOutlet UIImageView *electricCable_img_three;
@property (weak, nonatomic) IBOutlet UIImageView *electricCable_img_two;
@property (weak, nonatomic) IBOutlet UIImageView *electricCable_vertical;
@property (weak, nonatomic) IBOutlet UIImageView *electricCable_vertical_two;
@property (weak, nonatomic) IBOutlet UIImageView *electricCable_vertical_three;
@property (assign, nonatomic) float speed;
@end
@implementation ElectricWaveView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(void)awakeFromNib{
    [super awakeFromNib];
    self.speed =1.f;
    [self setElectricCableGroundAnimation];
    [self setElectricCableGroundAnimationTwo];
    [self setElectricCableGroundAnimationThree];
    [self setelectricCableVerticalGroundAnimation];
    [self setelectricCableVerticalTwoGroundAnimationTwo];
    [self setelectricCableVerticalThreeGroundAnimation];
}
-(void)addSpeedElectricWaveViewAnimation:(float)speed{//设置动画运行速度
    self.speed = speed;
    [self removeElectricWaveViewAnimation];
    [self setElectricCableGroundAnimation];
    [self setElectricCableGroundAnimationTwo];
    [self setElectricCableGroundAnimationThree];
    [self setelectricCableVerticalGroundAnimation];
    [self setelectricCableVerticalTwoGroundAnimationTwo];
    [self setelectricCableVerticalThreeGroundAnimation];
}
-(void)removeElectricWaveViewAnimation{
    [self.electricCable_img.layer removeAllAnimations];
    [self.electricCable_img_three.layer removeAllAnimations];
    [self.electricCable_img_two.layer removeAllAnimations];
    [self.electricCable_vertical.layer removeAllAnimations];
    [self.electricCable_vertical_two.layer removeAllAnimations];
    [self.electricCable_vertical_three.layer removeAllAnimations];
}
-(void)setElectricCableGroundAnimation{
    CABasicAnimation *momAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
    momAnimation.toValue = [NSNumber numberWithFloat:350];
    momAnimation.duration = 1.2f;
    momAnimation.removedOnCompletion = NO;
    momAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    CABasicAnimation * opacityAnima = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnima.fromValue = [NSNumber numberWithFloat:1.0f];
    opacityAnima.toValue = [NSNumber numberWithFloat:0.0f];
    opacityAnima.beginTime = 0.5f;
    opacityAnima.duration = 0.7f;
    opacityAnima.removedOnCompletion = NO;
    CAAnimationGroup *groupAnimation = [CAAnimationGroup animation];
    groupAnimation.animations = [NSArray arrayWithObjects:momAnimation,opacityAnima, nil];
    groupAnimation.duration = 2.7f;
    groupAnimation.speed = self.speed;
    groupAnimation.removedOnCompletion = NO;
    groupAnimation.repeatCount = MAXFLOAT;
    [self.electricCable_img.layer addAnimation:groupAnimation forKey:@"groupAnimation"];
}
-(void)setElectricCableGroundAnimationTwo{
    CABasicAnimation *momAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
    momAnimation.toValue = [NSNumber numberWithFloat:ScreenWidth - 44];
    momAnimation.duration = 1.0f;
    momAnimation.removedOnCompletion = NO;
    momAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    CABasicAnimation * opacityAnima = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnima.fromValue = [NSNumber numberWithFloat:1.0f];
    opacityAnima.toValue = [NSNumber numberWithFloat:0.0f];
    opacityAnima.beginTime = 0.8f;
    opacityAnima.duration = 0.2f;
    opacityAnima.removedOnCompletion = NO;
    CAAnimationGroup *groupAnimation = [CAAnimationGroup animation];
    groupAnimation.animations = [NSArray arrayWithObjects:momAnimation,opacityAnima, nil];
    groupAnimation.duration = 1.0f;
    groupAnimation.speed = self.speed;
    groupAnimation.removedOnCompletion = NO;
    groupAnimation.repeatCount = MAXFLOAT;
    [self.electricCable_img_two.layer addAnimation:groupAnimation forKey:@"groupAnimation"];
}
-(void)setElectricCableGroundAnimationThree{
    CABasicAnimation *momAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
    momAnimation.toValue = [NSNumber numberWithFloat:170];
    momAnimation.duration = 2.5f;
    momAnimation.removedOnCompletion = NO;
    momAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    CABasicAnimation * opacityAnimaStart = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnimaStart.fromValue = [NSNumber numberWithFloat:0.0f];
    opacityAnimaStart.toValue = [NSNumber numberWithFloat:1.0f];
    opacityAnimaStart.fillMode =kCAFillModeForwards;
    opacityAnimaStart.duration = 0.4f;
    opacityAnimaStart.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    opacityAnimaStart.removedOnCompletion = NO;
    CABasicAnimation * opacityAnima = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnima.fromValue = [NSNumber numberWithFloat:1.0f];
    opacityAnima.toValue = [NSNumber numberWithFloat:0.0f];
    opacityAnima.beginTime = 1.5f;
    opacityAnima.duration = 1.0f;
    opacityAnima.fillMode =kCAFillModeForwards;
    opacityAnima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    opacityAnima.removedOnCompletion = NO;
    CAAnimationGroup *groupAnimation = [CAAnimationGroup animation];
    groupAnimation.animations = [NSArray arrayWithObjects:opacityAnimaStart,momAnimation,opacityAnima, nil];
    groupAnimation.duration = 4.5f;
    groupAnimation.speed = self.speed;
    groupAnimation.removedOnCompletion = NO;
    groupAnimation.repeatCount = MAXFLOAT;
    [self.electricCable_img_three.layer addAnimation:groupAnimation forKey:@"groupAnimation"];
}
-(void)setelectricCableVerticalGroundAnimation{
    CABasicAnimation *momAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
    momAnimation.toValue = [NSNumber numberWithFloat:320];
    momAnimation.duration = 1.2f;
    momAnimation.removedOnCompletion = NO;
    momAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    CABasicAnimation * opacityAnima = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnima.fromValue = [NSNumber numberWithFloat:1.0f];
    opacityAnima.toValue = [NSNumber numberWithFloat:0.0f];
    opacityAnima.beginTime = 0.7f;
    opacityAnima.duration = 0.5f;
    opacityAnima.removedOnCompletion = NO;
    CAAnimationGroup *groupAnimation = [CAAnimationGroup animation];
    groupAnimation.animations = [NSArray arrayWithObjects:momAnimation,opacityAnima, nil];
    groupAnimation.duration = 1.2f;
    groupAnimation.speed = self.speed;
    groupAnimation.removedOnCompletion = NO;
    groupAnimation.repeatCount = MAXFLOAT;
    [self.electricCable_vertical.layer addAnimation:groupAnimation forKey:@"groupAnimation"];
}
-(void)setelectricCableVerticalTwoGroundAnimationTwo{
    CABasicAnimation *momAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
    momAnimation.toValue = [NSNumber numberWithFloat:320];
    momAnimation.duration = 1.2f;
    momAnimation.removedOnCompletion = NO;
    momAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    CABasicAnimation * opacityAnimaStart = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnimaStart.fromValue = [NSNumber numberWithFloat:0.0f];
    opacityAnimaStart.toValue = [NSNumber numberWithFloat:1.0f];
    opacityAnimaStart.fillMode =kCAFillModeForwards;
    opacityAnimaStart.duration = 0.2f;
    opacityAnimaStart.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    opacityAnimaStart.removedOnCompletion = NO;
    CABasicAnimation * opacityAnima = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnima.fromValue = [NSNumber numberWithFloat:1.0f];
    opacityAnima.toValue = [NSNumber numberWithFloat:0.0f];
    opacityAnima.beginTime = 1.0f;
    opacityAnima.duration = 0.2f;
    opacityAnima.fillMode =kCAFillModeForwards;
    opacityAnima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    opacityAnima.removedOnCompletion = NO;
    CAAnimationGroup *groupAnimation = [CAAnimationGroup animation];
    groupAnimation.animations = [NSArray arrayWithObjects:opacityAnimaStart,momAnimation,opacityAnima, nil];
    groupAnimation.duration = 2.7f;
    groupAnimation.speed = self.speed;
    groupAnimation.removedOnCompletion = NO;
    groupAnimation.repeatCount = MAXFLOAT;
    [self.electricCable_vertical_two.layer addAnimation:groupAnimation forKey:@"groupAnimation"];
}
-(void)setelectricCableVerticalThreeGroundAnimation{
    CABasicAnimation *momAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
    momAnimation.toValue = [NSNumber numberWithFloat:220];
    momAnimation.duration = 1.2f;
    momAnimation.removedOnCompletion = NO;
    momAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    CABasicAnimation * opacityAnima = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnima.fromValue = [NSNumber numberWithFloat:1.0f];
    opacityAnima.toValue = [NSNumber numberWithFloat:0.0f];
    opacityAnima.fillMode =kCAFillModeForwards;
    opacityAnima.beginTime = 0.7f;
    opacityAnima.duration = 0.5f;
    opacityAnima.removedOnCompletion = NO;
    CAAnimationGroup *groupAnimation = [CAAnimationGroup animation];
    groupAnimation.animations = [NSArray arrayWithObjects:momAnimation,opacityAnima, nil];
    groupAnimation.duration = 3.9f;
    groupAnimation.speed = self.speed;
    groupAnimation.removedOnCompletion = NO;
    groupAnimation.repeatCount = MAXFLOAT;
    [self.electricCable_vertical_three.layer addAnimation:groupAnimation forKey:@"groupAnimation"];
}
@end
